<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/23 0023
 * Time: 下午 2:34
 */

namespace app\moonlighting\service;


use app\exception\AdminException;
use think\Request;
use app\sports_admin\model\Login as LoginModel;
class Login
{
    /**
     * 登录验证
     */
    public static function checkLogin()
    {
        $params  = Request::instance()->only(['username','pwd']);
        $adminInfo = LoginModel::findAdmin($params['username']);
        if(!$adminInfo){
            throw new AdminException([
                'msg'=>'账号不存在',
                'errorCode'=>'404'
            ]);
        }
        $adminInfo = $adminInfo->toArray();
        if($params['pwd'] != authcode($adminInfo['pwd'],'DECODE',$adminInfo['login'],0)){
            throw new AdminException([
                'msg'=>'账号或密码错误',
                'errorCode'=>'400'
            ]);
        }
        $scope = self::getAdminRule($adminInfo['id']);
        $adminInfo['scope'] = $scope;
        return $adminInfo;
    }
    /**
     * 获取管理员权限
     * @param $adminId 管理员id
     */
    public static function getAdminRule($adminId)
    {
        $rule = LoginModel::getAdminRule($adminId);
        return $rule->admin_rule->get_rule->scope;
    }
}